package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.User;
import com.qk.entity.domain.user.UserDO;
import com.qk.entity.dto.user.UserDTO;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fantuan
 * @created 2025/10/7 18:14
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增用户
     *
     * @param user 用户信息
     * @return 统一响应结果
     */
    @PostMapping
    public Result addUser(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids) {
        userService.deleteById(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        UserDO userDO = userService.getUserById(id);
        return Result.success(userDO);
    }

    @PutMapping
    public Result updateUser(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectAllUsers() {
        return Result.success(userService.selectAllUsers());
    }

    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }

    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        return Result.success(userService.listByDept(deptId));
    }
}
